var url = require('url');
var template = require('templateConfig');
var main = require('main');
var form = require('form');
var module= require('module');

//模板配置
var tplArr = [];
var cartTpl =require('raw!../template/shop-cart.txt'); 
tplArr['cart'] = template.compile(cartTpl.replace(/^\s*|\s*$/g, "")); 
 
function Index(){
    _this = this;
    this.main = function(){
        main.event(this,'tap','i-click'); 
        main.event(this,'change','i-change'); 
        this.init();
    }     

    this.getContent= function(getUrl, changeUrl, deleteUrl) {
        main.post(getUrl, {}, function(ret){  
            if(ret.code!= 1) {
                $(".content").html("购物车为空!");
                return false;
            };
            var dataPrice= {
                tType: "price",
                price: "0",
                reduce: "0.00",
                checkSingle: true
            }; 
            var dataOrder= {
                tType: "orderList",
                dataList: []
            }    
            for(var n in ret.data){
                var _json= ret.data[n];
                var _data= {
                        "cart_id": _json.cart_id,
                        "id": _json.good_id,
                        "title":_json.good_name,
                        "attr": [],
                        "num":_json.good_sku,
                        "spec":_json.colour+ ","+ _json.cup+ ","+ _json.size, 
                        "price":_json.shop_price,
                        "image":_json.small_image,
                        "n":_json.good_number
                };
                dataOrder.dataList.push(_data); 
            }      	
        	var data= {
        		tType: "content",
        		price: tplArr['cart'](dataPrice),
        		orderList: tplArr['cart'](_this.getOrderList(dataOrder)),
        		link: '',//_this.jumpUrl,
        		num: "0"
        	}
        	$(".content").html(tplArr['cart'](data))
        })    
    }

    this.getOrderList= function(data){ 
    	var ret= data;
    	$.each(ret.dataList, function(i, v){  
    	 	var r= [];
    	 	r.push(v);
    		v.content= module.ordersGoodsInfo(r); 
    	})  
    	return ret;
    }

    //更换购物车数量
    this.changeCount= function(ths, type){
        if(!!_this.addAjax) {
            console.info("频次太快");
            return false;
        }
        if(type!=1 &&ths.siblings("input").val()=="1"){
            return false;
        }
        _this.addAjax= true;
        var num= parseInt(ths.siblings("input").val());
        if(type== 1){
            ths.siblings("input").val(++num);
        }
        else if(num> 1) {
            ths.siblings("input").val(--num);
        }
        _this.choosePro(); 
        var _id= ths.attr("cart");
        main.post(_this.changeUrl, {id: _id, num: num}, function(ret){ 
            if(ret.code== 1){             
                _this.addAjax= false;
            }
        })       
    }

    //选中商品,修改价格,修改链接
    this.choosePro= function(ths, id){ 
        var _input= $(".mw>input"), num= 0, price=0, ids='';
        $.each(_input, function(i, v){ 
            if(!!$(this).prop("checked")){
                num++;
                var dv= $(this).parent(".mw").siblings(".orders-goods-info");
                var p= dv.find(".proprice").html(), n= dv.find(".pronum").val();
                // console.info(p, n)
                price+= parseFloat(p)*parseFloat(n);
                ids+= $(this).attr("cart")+","; 
            }
            else {
                $("#all").prop("checked", false);
            }
        }); 
        $(".pronumber").html(num);
        if(!price) price= 0;
        $(".propriceall").html(price); 
        if(!ids) {
            $(".gotoConfirm").attr("href", "");
        }  
        else {
            $(".gotoConfirm").attr("href", _this.jumpUrl+ "?id="+ids);            
        }     
    }
    //全选
    this.chooseAll= function(ths){
        if(!!ths.prop("checked")){
            $(".mw>input").prop("checked", true);
        }
        else {
            $(".mw>input").prop("checked", false);
        }
        _this.choosePro();
    }
    
    //删除购物车
    this.deleteCart= function(ths, id){
        main.post(_this.deleteUrl, {id, id}, function(ret){
            if(ret.code== 1){
                $(ths).parents("li.orders-goods-info").remove();
                _this.choosePro();
            }
        })
    }

    this.init= function(){  
        this.getUrl= url.postCartList;
        this.changeUrl= url.postUpdateCartNum;
        this.deleteUrl= url.postDestroyCart;
        this.jumpUrl= "order-confirm";
        this.getContent(this.getUrl, this.changeUrl, this.deleteUrl);  
        this.addAjax= false; 
    }
    return this.main();
};

var index = new Index();